﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<TireBias.Models.BIASPLANModel.BIASPLAN_DS>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    unLockTopping
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="FeaturedContent" runat="server">
    <section class="featured">
        <div class="content-wrapper" style=" min-height: 30px; ">
            <hgroup class="title float-left">
                <h1>BIAS NO.</h1> <h2><%: ViewBag.Biasno %></h2>
            </hgroup>           
            <hgroup class="title float-right">
                <h1>Plan Date : <%: ViewBag.dtPlan %></h1>
            </hgroup>           

        </div>
    </section>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div style="clear: left">
        <div class="cap shadow txt13bold">
            Import date :
        </div>
        <div id="txtImp" class="desc txt13bold" style="padding-right: 10px;">
            <input type="text" id="dFrom" value="" />
        </div>
        <button id="search">
        </button>
    </div>
    <div style="clear: left;">
        <ul id="menu1" class="menu1" style="vertical-align: bottom;">
            <li>
                <%: Html.ActionLink("Back", "Index", new {  dtPlan = ViewBag.dtPlan , bias = ViewBag.Biasno  })%></li>
            <li class="above"><a href="#">Insert Above line </a></li>
            <li class="under"><a href="#">Insert Under line</a></li>
            <li id="save"><a href="#">Save</a></li>
        </ul>
        &nbsp;&nbsp;
    </div>
    <table width="100%" id="dg">
        <thead>
            <tr class="head" id="trRow">
                <td class="cell " style="width: 50px;">
                    STATUS
                </td>
                <td class="cell" style="width: 120px;">
                    SEQ
                </td>
                <td class="cell txt" style="width: 150px;">
                    TOPPED CORD
                </td>
                <td class="cell txt" style="width: 150px;">
                    SIZE
                </td>
                <td class="cell txt" style="width: 70px;">
                    P1
                </td>
                <td class="cell txt" style="width: 75px;">
                    P2
                </td>
                <td class="cell txt" style="width: 75px;">
                    P3
                </td>
                <td class="cell txt" style="width: 75px;">
                    P4
                </td>
                <td class="cell txt" style="width: 120px;">
                    FORMING
                </td>
                <td class="cell txt" style="width: 120px;">
                    TEXT
                </td>
                <td class="cell" style="text-align: center; width: 50px;">
                    DELETE
                </td>
            </tr>
        </thead>
        <tbody>
            <%
                int i = 0;

                foreach (var item in Model)
                {
                    i = i + 1;
                    string trrow = "rowa";
                    if (i % 2 == 0) trrow = "rowb";
                    string seq = item._Plandt + '-' + item._Itemno;

                    if (item._Plants == "U")
                    {   
            %>
            <tr class="rowa" style="line-height: 25px;">
                <td class="cell" style="width: 50px; text-align: center;">
                    <%=item._Plants%>
                </td>
                <td style="width: 120px; text-align: center;" class="cell">
                    <%= seq%>
                </td>
                <td style="width: 150px; text-align: center;" class="cell txt">
                    <input type="text" value="<%= item._Topcrdcd%>" style="width: 100%;" class="sort1"
                        maxlength="30" />
                </td>
                <td style="width: 150px;" class="cell txt">
                    <input type="text" value="<%= item._Size %>" style="width: 100%;" maxlength="30"
                        class="sort2" />
                </td>
                <td style="text-align: center;" class="cell txt">
                    <input type="text" value="<%= item._P1 %>" style="width: 100%;" maxlength="6" class="right" />
                </td>
                <td style="text-align: center;" class="cell txt">
                    <input type="text" value="<%= item._P2 %>" style="width: 100%;" maxlength="6" class="right" />
                </td>
                <td style="text-align: center;" class="cell txt">
                    <input type="text" value="<%= item._P3 %>" style="width: 100%;" maxlength="6" class="right" />
                </td>
                <td style="text-align: center;" class="cell txt">
                    <input type="text" value="<%= item._P4 %>" style="width: 100%;" maxlength="6" class="right" />
                </td>
                <td style="text-align: center;" class="cell txt">
                    <input type="text" value="<%= item._Forming %>" style="width: 100%;" maxlength="5"
                        class="sort3" />
                </td>
                <td style="text-align: center;" class="cell txt">
                    <input type="text" value="<%= item._Text %>" style="width: 100%;" maxlength="255"
                        class="right" />
                </td>
                <td style="width: 50px; text-align: center;" class="cell">
                    <img src="<%: Url.Content("~/Images/action_delete.png") %>" class="del" alt='' />
                </td>
            </tr>
            <% }
               else
               { %>
            <tr class="rowb" style="line-height: 25px;">
                <td style="width: 50px; text-align: center;">
                    <%:  item._Plants%>
                </td>
                <td style="width: 120px; text-align: center;">
                    <%= seq%>
                </td>
                <td style="width: 150px; text-align: center;">
                    <%:  item._Topcrdcd%>
                </td>
                <td style="width: 150px;">
                    <%:  item._Size %>
                </td>
                <td style="width: 75px; text-align: center;">
                    <%:  item._P1%>
                </td>
                <td style="width: 75px; text-align: center;">
                    <%:  item._P2%>
                </td>
                <td style="width: 75px; text-align: center;">
                    <%:  item._P3%>
                </td>
                <td style="width: 75px; text-align: center;">
                    <%:  item._P4%>
                </td>
                <td style="width: 120px; text-align: center;">
                    <%:  item._Forming%>
                </td>
                <td style="width: 120px; text-align: center;">
                    <%:  item._Text %>
                </td>
                <td>
                </td>
            </tr>
            <% }
            } %>
        </tbody>
    </table>
</asp:Content>
<asp:Content ID="idTableContent" ContentPlaceHolderID="TableContent" runat="server">
    <div id="dialog-file"  title="File ที่ไม่พบในระบบ">
        <table id="file" style="width:100%">
            <thead>
                <tr id="trhead"class="head">
                    <td  class=" cell">
                        TireCode
                    </td>
                    <td  class=" cell">
                        Size
                    </td>
                    <td  class=" cell">
                        Forming
                    </td>
                    <td  class=" cell">
                        Folder
                    </td>
                    <td  class=" cell">
                        File
                    </td>
                </tr>
            </thead>
            <tbody>
            
            </tbody>
        </table>
    </div>
    <div style="clear: both;">
        <table class="shadow" id="UseTbl" style="text-align: left; margin: 0px 0px 0px 0px;">
            <thead>
                <tr class="cell" style="background-color: #ccc; color: #000; font-weight: bold; text-align: center;">
                    <td colspan="9">
                        USED PLAN
                    </td>
                </tr>
                <tr class="head" id="trUse" style="background-color: #008080; color: White;">
                    <td class="cell" style="width: 120px;">
                        SEQ
                    </td>
                    <td class="cell" style="width: 150px;">
                        TOPPED CORD
                    </td>
                    <td class="cell" style="width: 150px;">
                        SIZE
                    </td>
                    <td class="cell" style="width: 50px;">
                        P1
                    </td>
                    <td class="cell" style="width: 50px;">
                        P2
                    </td>
                    <td class="cell" style="width: 50px;">
                        P3
                    </td>
                    <td class="cell" style="width: 50px;">
                        P4
                    </td>
                    <td class="cell" style="width: 120px;">
                        FORMING
                    </td>
                    <td class="cell" style="width: 120px;">
                        TEXT
                    </td>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="ScriptsSection" runat="server">
    <style type="text/css">
        .right
        {
        }
        input[type="text"]
        {
            width: 100%;
        }
        .skip
        {
            color: Red !important;
        }
        .samp
        {
            color: #FF6D00 !important;
        }
        .nfin
        {
            color: Blue !important;
        }
    </style>
    <% if (false)
       { %>
    <script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
    <% } %>
    <script type="text/javascript">

        var input = $("#dg tr.rowa").find("input:text").live("keyup", function () {
            this.value = this.value.toUpperCase();
        });
        function code_sort(obj1, obj2, obj3) {

            $.post('<%= Url.Action("GET_CODE") %>', function (data) {
                obj1.autocomplete({
                    source: data,
                    change: function (event, ui) {
                        obj2.val("");
                        obj3.val("");
                        var source = $(this).val();
                        $.post('<%= Url.Action("GET_CODE") %>' + '?code=' + source + '&sel=sizes', function (data) {
                            obj2.autocomplete({
                                source: data,
                                change: function (event, ui) {
                                    obj3.val("");
                                    var source = $(this).val();
                                    $.post('<%= Url.Action("GET_CODE") %>' + '?sizes=' + source + '&sel=forming', function (data) {
                                        obj3.autocomplete({
                                            source: data,
                                            change: function (event, ui) {
                                                var source = $(this).val();

                                            } //change
                                        }); //a    
                                    }); //post

                                } //change
                            }); //a    
                        });
                    } //change              
                }); //a
            });
        }

        //         code_sort($("#text1"), $("#text2"), $("#text3"));
        $(".sort2").live("click", function () {

            obj2 = $(this);
            obj3 = $(this).parent().parent().find(".sort2")
            $(this).parent().parent().find(".sort3").val("");
            code = ($(this).parent().parent().find(".sort1")).val();
            $.post('<%= Url.Action("GET_CODE") %>' + '?code=' + code + '&sel=sizes', function (data) {
                obj2.autocomplete({
                    source: data
                });
            });
        });
        $(".sort3").live("click", function () {
            obj3 = $(this);
            code = ($(this).parent().parent().find(".sort2")).val();
            $.post('<%= Url.Action("GET_CODE") %>' + '?sizes=' + code + '&sel=forming', function (data) {
                obj3.autocomplete({
                    source: data
                });
            });
        });

        $('#dFrom').dateEntry({ useMouseWheel: false, dateFormat: 'dmy/' });

        $("#search").button({
            icons: { primary: 'ui-icon-arrowthickstop-1-s' }, text: false
        }).click(function () {
            var dFrom = $("#dFrom").val();
            $.post('<%=Url.Action("Import")%>', { dtPlan: dFrom, biasno: '<%: ViewBag.Biasno %>' }, function (data) {

                WAITINGDLG_show(imgWait);

                for (i = 0; i < data.length; i++) {
                    var tr = $("#dg tr.head").clone(true).removeClass("head").addClass("rowa").removeAttr("id");
                    tr.find(".cell").each(function () {

                        if ($(this).hasClass("txt")) {
                            if ($.trim($(this).text()) == 'TOPPED CORD') {
                                $(this).html(txt.format(data[i]._Topcrdcd, 30, 100, 'sort1'));
                            }
                            else if ($.trim($(this).text()) == 'SIZE') {
                                $(this).html(txt.format(data[i]._Size, 30, 100, 'sort2'));
                            }
                            else if ($.trim($(this).text()) == 'FORMING') {
                                $(this).html(txt.format(data[i]._Forming, 5, 100, 'sort3'));
                            }
                            else if ($.trim($(this).text()) == 'TEXT') {
                                $(this).html(txt.format(data[i]._Text, 255, 100));
                            }
                            else if ($.trim($(this).text()) == 'P1') {
                                $(this).html(txt.format(data[i]._P1, 255, 100));
                            }
                            else if ($.trim($(this).text()) == 'P2') {
                                $(this).html(txt.format(data[i]._P2, 255, 100));
                            }
                            else if ($.trim($(this).text()) == 'P3') {
                                $(this).html(txt.format(data[i]._P3, 255, 100));
                            }
                            else if ($.trim($(this).text()) == 'P4') {
                                $(this).html(txt.format(data[i]._P4, 255, 100));
                            }
                        } else $(this).text("");

                        var p = '<%: Url.Content("~/Images/action_delete.png") %>'
                        tr.find("td:last").html("<img class='del' src='" + p + "' />");
                        code_sort(tr.find("input:text:eq(0)"), tr.find("input:text:eq(1)"), tr.find("input:text:eq(6)"));
                        tr.appendTo($("#dg tr.rowa:last").parent());
                    });
                }
                WAITINGDLG_hide();
            });
        });

        /******* CREATE ROW **********/
        var txt = "<input type='text' value='{0}' maxlength='{1}' style=\"width: {2}%; text-align:center;  \" class='{3}' />";
        String.prototype.format = function () {
            var args = arguments;
            return this.replace(/\{(\d+)\}/g, function (m, n) { return args[n]; });
        };
        function CreatTR() {
            var tr = $("#dg tr.head").clone(true).removeClass("head").addClass("rowa").removeAttr("id");
            tr.find(".cell").each(function () {

                if ($(this).hasClass("txt")) {
                    if ($.trim($(this).text()) == 'TOPPED CORD') {

                        $(this).html(txt.format('', 30, 100));
                    }
                    else if ($.trim($(this).text()) == 'SIZE') {
                        $(this).html(txt.format('', 30, 100));
                    }
                    else if ($.trim($(this).text()) == 'FORMING') {
                        $(this).html(txt.format('', 5, 100));
                    }
                    else if ($.trim($(this).text()) == 'TEXT') {
                        $(this).html(txt.format('', 255, 100));
                    }
                    else {
                        $(this).html(txt.format('', 6, 97));
                        $(this).find("input:text").setMask("999.99").addClass("right");
                    }
                }
                else $(this).text("");

            });
            var p = '<%: Url.Content("~/Images/action_delete.png") %>'

            code_sort(tr.find("input:text:eq(0)"), tr.find("input:text:eq(1)"), tr.find("input:text:eq(6)"));
            tr.find("td:last").html("<img class='del' src='" + p + "' />");
            return tr;
        }

        $(function () {
            /******* INSERT ROW **********/
            $(".under,.above").live("click", function (event) {
                var $tr = $("tr.actived:first");
                var tr = CreatTR();

                if ($tr.length > 0) {
                    if ($(this).hasClass("above")) {
                        $tr.before(tr);
                    } else if ($(this).hasClass("under")) {
                        $tr.after(tr);
                    }
                }
                else {
                    tr.appendTo($("#dg tr.rowa:last").parent());

                }
            });
            /************ DEL ROW ******************/
            $(".del").live("click", function () {
                $(this).closest("tr").remove();

                if ($("#dg tr.rowa").length <= 1) {
                    var tr = CreatTR();
                    tr.appendTo($("#dg tr:last"));
                    return
                }


            });

            /******* SET INITAIL **********/
            setTimeout(function () {

                if ($("#dg tr.rowa:first").length <= 0) {
                    $("#dg").append(CreatTR());
                } else {
                    $("#dg tr.rowa").each(function () {
                        code_sort($(this).find("input:text:eq(0)"), $(this).find("input:text:eq(1)"), $(this).find("input:text:eq(6)"));
                        $(this).find("input:text:eq(2),input:text:eq(3),input:text:eq(4),input:text:eq(5)").setMask("999.99");
                    });
                }

                $("#dg tr.rowa,#dg tr.rowb ").live("click", function () {

                    $("tr.rowa, tr.rowb").removeClass("actived");
                    var $tr = $(this);
                    $tr.addClass("actived");

                });
            }, 500)

            /******* SAVE DATA **********/

            $("#save").click(function () {
                var bln = true;
                var dataForSubmit = new Array();
                WAITINGDLG_show(imgWait);
                $("#dg tr.rowa").each(function () {
                    var $tr = $(this);
                    if (!bln) return;

                    var Topcrdcd = $.trim($tr.find("input:eq(0)").val());
                    var Sizes = $.trim($tr.find("input:eq(1)").val());
                    var P1 = $.trim($tr.find("input:eq(2)").val());
                    var P2 = $.trim($tr.find("input:eq(3)").val());
                    var P3 = $.trim($tr.find("input:eq(4)").val());
                    var P4 = $.trim($tr.find("input:eq(5)").val());
                    var Forming = $.trim($tr.find("input:eq(6)").val());
                    var Text = $.trim($tr.find("input:eq(7)").val());


                    //GET===>SaveData 
                    if ($tr.parent().find("td.cell:first").html() != "L") {
                        var str = (Topcrdcd + Sizes + P1 + P2 + P3 + P4 + Forming + Text);
                        if (str != "") {
                            if (Topcrdcd == "") {
                                alert("ระบูค่าไม่ครบ");
                                $tr.find("input:eq(0)").focus();
                                bln = false;
                            }
                            else if (Sizes == "") {
                                alert("ระบูค่าไม่ครบ");
                                $tr.find("input:eq(1)").focus();
                                bln = false;
                                return;
                            } else if ((P1 == "") && (P2 == "") && (P3 == "") && (P4 == "")) {
                                alert("ระบุค่าไม่ครบ");
                                $tr.find("input:eq(2)").focus();
                                bln = false;
                                return;
                            }
                            else if (Forming == "") {
                                alert("ระบุค่าไม่ครบ");
                                $tr.find("input:eq(6)").focus();
                                bln = false;
                                return;
                            }
                            //                             else if (Text == "") {
                            //                                 alert("ระบุค่าไม่ครบ");
                            //                                 $tr.find("input:eq(7)").focus();
                            //                                 bln = false;
                            //                                 return;
                            //                             }

                            dataForSubmit.push({
                                _Biasno: '<%= ViewBag.Biasno %>',
                                _Plandt: "<%=  ViewBag.dtPlan %>",
                                _Itemno: $tr.parent().find("td.cell:eq(1)").html(),
                                _Topcrdcd: $tr.find("input:eq(0)").val(),
                                _Size: $tr.find("input:eq(1)").val(),
                                _P1: $tr.find("input:eq(2)").val(),
                                _P2: $tr.find("input:eq(3)").val(),
                                _P3: $tr.find("input:eq(4)").val(),
                                _P4: $tr.find("input:eq(5)").val(),
                                _Forming: $tr.find("input:eq(6)").val(),
                                _Text: $tr.find("input:eq(7)").val()
                            });
                        }
                    }
                });
                var getUsersUrl = '<%= Url.Action("Add_Edit_UNLock") %>';

                if (bln) callScriptMethod(getUsersUrl, { theData: dataForSubmit, dtPlan: '<%= ViewBag.dtPlan %>', bias: '<%= ViewBag.Biasno %>' }, 'ADD');
            });


            $("#dialog-file").dialog({
                width: 800,
                height: 300,
                position: {
                    my: 'center',
                    at: 'center'

                },
                autoOpen: false,
                show: "blind"
            });

            $.post('<%=Url.Action("Get_USE_ACTIVE") %>' + '?bias=' + '<%= ViewBag.Biasno %>', function (data) {

                WAITINGDLG_show(imgWait);
                $("#UseTbl tbody").empty();

                for (i = 0; i < data.length; i++) {
                    var seq = data[i]._Plandt + '-' + data[i]._Itemno;
                    var row = "rowa";
                    if (i % 2 == 0) {
                        row = "rowa";
                    }
                    var $tr = $("#trUse").clone().addClass(row).removeAttr("style");
                    $("td:eq(0)", $tr).text(seq).addClass("text-center");
                    $("td:eq(1)", $tr).text(data[i]._Topcrdcd).addClass("text-center");
                    $("td:eq(2)", $tr).text(data[i]._Size).addClass("text-center");
                    $("td:eq(3)", $tr).text(data[i]._P1).addClass("text-center");
                    $("td:eq(4)", $tr).text(data[i]._P2).addClass("text-center");
                    $("td:eq(5)", $tr).text(data[i]._P3).addClass("text-center");
                    $("td:eq(6)", $tr).text(data[i]._P4).addClass("text-center");
                    $("td:eq(7)", $tr).text(data[i]._Forming).addClass("text-center");
                    $("td:eq(8)", $tr).text(data[i]._Text).addClass("text-center");
                    if (data[i]._Skpflag != "") {
                        $tr.addClass("skip");
                    }
                    if (data[i]._nfinemp != "") {
                        $tr.addClass("nfin");
                    }
                    if (data[i]._Smpemp != "") {
                        $tr.addClass("samp");
                    }

                    $tr.appendTo($("#UseTbl tbody"));
                }
                WAITINGDLG_hide();
            });
            /********* AJAX ********/

            function callScriptMethod(url, jsonObject, action, callback, async) {

                callback = callback || function () { };
                async = (async == null || async);

                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: url,
                    data: JSON.stringify(jsonObject),
                    dataType: "json",
                    async: async,
                    success: function (data) {
                        WAITINGDLG_hide();
                        switch (action) {

                            case 'ADD':
                                alert(data.message);
                                if (data.success != "0") {
                                    $("#sid").val(data.success);
                                    window.location = '<%: Url.Action("unLockTopping") %>' + '?dtPlan=' + '<%= ViewBag.dtPlan %>' + '&bias=' + '<%= ViewBag.Biasno %>';
                                }
                                if (data.list.length > 0) {
                                    for (i = 0; i < data.list.length; i++) {
                                        var tr = $("#trhead").clone(true).removeClass("head").addClass("rowb").removeAttr("id");
                                        $("td:eq(0)", tr).text(data.list[i]._TireCode)
                                        $("td:eq(1)", tr).text(data.list[i]._Sizes)
                                        $("td:eq(2)", tr).text(data.list[i]._Forming)
                                        $("td:eq(3)", tr).text(data.list[i]._SubFd)
                                        $("td:eq(4)", tr).text(data.list[i]._Filenm)
                                        tr.appendTo($("#file tbody").parent());
                                    }
                                    $("#dialog-file").dialog("open");
                                }
                                if (jQuery.type(data.rows) != "undefined") {

                                    $("#dg tr.rowa").eq(data.rows).find("input").eq(data.item).focus();
                                }
                                break;
                            case 'GET':
                                break;
                        }
                    },
                    error: function (xhr, status, error) {
                        var err = xhr.responseText;
                        alert(err.Message);
                    }
                });
            }
        });
    </script>
</asp:Content>
